<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>查询学过单词 - 单词记忆</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background: linear-gradient(135deg, #e8f4f8 0%, #f0f8fb 100%);
            background-image:
                radial-gradient(circle at 20% 30%, rgba(64, 158, 255, 0.08) 0%, transparent 25%),
                radial-gradient(circle at 80% 70%, rgba(64, 158, 255, 0.08) 0%, transparent 25%);
            background-attachment: fixed;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        .container {
            background-color: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
            padding: 40px 30px;
            max-width: 800px;
            width: 100%;
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.8);
            min-height: 500px;
        }
        .icon {
            font-size: 80px;
            margin-bottom: 30px;
            display: block;
        }
        h1 {
            color: #2c3e50;
            font-size: 28px;
            margin-bottom: 10px;
        }
        .description {
            color: #606266;
            font-size: 14px;
            margin-bottom: 20px;
        }
        .back-btn {
            padding: 12px 30px;
            background: linear-gradient(90deg, #409eff, #67c2f9);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
        }
        .back-btn:hover {
            background: linear-gradient(90deg, #3088e0, #409eff);
            box-shadow: 0 6px 16px rgba(64, 158, 255, 0.3);
            transform: translateY(-2px);
        }
        /* 排序按钮组 */
        .sort-buttons {
            display: flex;
            gap: 10px;
            justify-content: center;
            margin: 30px 0 20px;
            flex-wrap: wrap;
        }
        .sort-btn {
            padding: 10px 20px;
            background: linear-gradient(135deg, #ffffff 0%, #f9fafb 100%);
            border: 2px solid #e0e6ed;
            border-radius: 8px;
            color: #606266;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            font-weight: 500;
        }
        .sort-btn:hover {
            border-color: #409eff;
            color: #409eff;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(64, 158, 255, 0.1);
        }
        .sort-btn.active {
            background: linear-gradient(90deg, #409eff, #67c2f9);
            border-color: #409eff;
            color: white;
            box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
        }
        /* 单词列表 */
        .word-list {
            max-height: 400px;
            overflow-y: auto;
            margin: 20px 0;
            padding: 10px;
        }
        .word-item {
            background: linear-gradient(135deg, #ffffff 0%, #f9fafb 100%);
            padding: 15px 20px;
            margin-bottom: 12px;
            border-radius: 8px;
            border-left: 4px solid #409eff;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .word-item:hover {
            transform: translateX(5px);
            box-shadow: 0 4px 12px rgba(64, 158, 255, 0.15);
            border-left-color: #67c2f9;
        }
        .word-name {
            font-size: 18px;
            font-weight: 600;
            color: #2c3e50;
        }
        .word-time {
            font-size: 14px;
            color: #909399;
        }
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #909399;
        }
        .empty-state::before {
            content: '📚';
            display: block;
            font-size: 60px;
            margin-bottom: 20px;
            opacity: 0.5;
        }
        .loading {
            text-align: center;
            padding: 40px;
            color: #909399;
        }
        .loading-spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(64, 158, 255, 0.2);
            border-top-color: #409eff;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            margin-right: 10px;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
    <script>
        let currentSortType = 'time_asc';
        
        async function checkLogin() {
            try {
                const response = await fetch('/api/user', { credentials: 'include' });
                if (!response.ok) {
                    window.location.href = '/login.html';
                    return false;
                }
                return true;
            } catch (err) {
                window.location.href = '/login.html';
                return false;
            }
        }
        
        // 加载已学单词列表
        async function loadLearnedWords(sortType = 'time_asc') {
            const wordList = document.getElementById('wordList');
            wordList.innerHTML = '<div class="loading"><span class="loading-spinner"></span>加载中...</div>';
            
            try {
                const response = await fetch(`/api/learned-words?sortType=${sortType}`, {
                    credentials: 'include'
                });
                
                const result = await response.json();
                
                if (!result.success) {
                    wordList.innerHTML = `<div class="empty-state">加载失败：${result.message}</div>`;
                    return;
                }
                
                const words = result.words;
                
                if (words.length === 0) {
                    wordList.innerHTML = '<div class="empty-state">暂无已学单词</div>';
                    return;
                }
                
                // 渲染单词列表
                wordList.innerHTML = words.map(word => {
                    const learnedTime = new Date(word.learned_time).toLocaleString('zh-CN');
                    return `
                        <div class="word-item" onclick="goToWordDetail('${word.bookId}')">
                            <span class="word-name">${word.headWord}</span>
                            <span class="word-time">${learnedTime}</span>
                        </div>
                    `;
                }).join('');
                
            } catch (err) {
                console.error('加载已学单词失败：', err);
                wordList.innerHTML = '<div class="empty-state">网络错误，请重试</div>';
            }
        }
        
        // 切换排序方式
        function changeSort(sortType) {
            currentSortType = sortType;
            
            // 更新按钮样式
            document.querySelectorAll('.sort-btn').forEach(btn => {
                btn.classList.remove('active');
            });
            document.querySelector(`[data-sort="${sortType}"]`).classList.add('active');
            
            // 重新加载数据
            loadLearnedWords(sortType);
        }
        
        // 跳转到单词详情页
        function goToWordDetail(bookId) {
            window.location.href = `/index.html?bookId=${bookId}`;
        }
        
        window.onload = async () => {
            const isLoggedIn = await checkLogin();
            if (isLoggedIn) {
                loadLearnedWords(currentSortType);
            }
        };
    </script>
</head>
<body>
<div class="container">
    <span class="icon">📝</span>
    <h1>已学单词</h1>
    <p class="description">查看你已掌握的单词列表</p>
    
    <!-- 排序按钮组 -->
    <div class="sort-buttons">
        <button class="sort-btn active" data-sort="time_asc" onclick="changeSort('time_asc')">时间正序</button>
        <button class="sort-btn" data-sort="time_desc" onclick="changeSort('time_desc')">时间逆序</button>
        <button class="sort-btn" data-sort="letter_asc" onclick="changeSort('letter_asc')">字母 A-Z</button>
        <button class="sort-btn" data-sort="letter_desc" onclick="changeSort('letter_desc')">字母 Z-A</button>
    </div>
    
    <!-- 单词列表 -->
    <div class="word-list" id="wordList">
        <div class="loading"><span class="loading-spinner"></span>加载中...</div>
    </div>
    
    <button class="back-btn" onclick="window.location.href='/home.html'">返回首页</button>
</div>
</body>
</html>
